package itman.elm.dao.impl;

import itman.elm.dao.BusinessDao;
import itman.elm.po.Business;
import itman.elm.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {

    private Connection conn = null;
    private PreparedStatement pstm = null;
    private ResultSet rs = null;

    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws Exception {

        List<Business> list = new ArrayList<>();

        String sql = "select * from business where orderTypeId=? order by businessId";
        try{
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql);

            //获取参数
            pstm.setInt(1, orderTypeId);

            rs = pstm.executeQuery();
            while (rs.next()){
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImp(rs.getString("businessImp"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarprice(rs.getDouble("StarPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                list.add(business);
            }

        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pstm);
        }
        return list;
    }

    @Override
    public Business getBusinessById(Integer businessId) throws Exception {
        Business business = null;
        String sql = "select * from business where businessId=?";
        try {
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1, businessId);

            rs = pstm.executeQuery();
            if(rs.next()) {
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImp(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarprice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
            }
        }finally {
            DBUtil.close(rs, pstm);
        }
        return business;
    }
}
